package net.openl10n.api.document;

import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

import net.openl10n.api.Namespaces;

@XmlRootElement(name="document", namespace=Namespaces.DOCUMENT)
public class Document {

	private String id;
	private String name;
	private String path;
	private String contentType;
	private Integer version = 1;
	private String lang = "en-US";

	private List<DocumentTarget> targets;
	
	public Document() {
	}

	public Document(String fullPath, String contentType){
		int lastSepChar =  fullPath.lastIndexOf('/');
		switch(lastSepChar){
		case -1:
			this.path = "";
			this.id = this.name = fullPath;
			break;
		case 0:
			this.path = "/";
			this.id = fullPath;
			this.name = fullPath.substring(1);
			break;
		default:
			this.path = fullPath.substring(0,lastSepChar+1);
			this.id = fullPath;
			this.name = fullPath.substring(lastSepChar+1);
		}
		this.contentType = contentType;
	}
	
	public Document(String fullPath, String contentType, Integer version){
		this(fullPath, contentType);
		this.version = version;
	}	
	
	public Document(String fullPath, String contentType, Integer version, String lang){
		this(fullPath, contentType, version);
		this.lang = lang;
	}	

	public Document(String id, String name, String path, String contentType) {
		this.id = id;
		this.name = name;
		this.path = path;
		this.contentType = contentType;
	}

	public Document(String id, String name, String path, String contentType, Integer version) {
		this(id, name, path, contentType);
		this.version = version;
	}
	
	public Document(String id, String name, String path, String contentType, Integer version, String lang) {
		this(id, name, path, contentType, version);
		this.lang = lang;
	}
	
	@XmlAttribute(name="id", required=true)
	public String getId() {
		return id;
	}
	
	public void setId(String id) {
		this.id = id;
	}
	
	@XmlAttribute(name="name", required=true)
	public String getName() {
		return name;
	}
	
	public void setName(String name) {
		this.name = name;
	}
	
	@XmlAttribute(name= "path", required=false)
	public String getPath() {
		return path;
	}
	
	public void setPath(String path) {
		this.path = path;
	}

	@XmlAttribute(name="version", required=true)
	public Integer getVersion() {
		return version;
	}
	
	public void setVersion(Integer version) {
		this.version = version;
	}

	@XmlAttribute(name="content-type", required=true)
	public String getContentType() {
		return contentType;
	}
	
	
	public void setContentType(String contentType) {
		this.contentType = contentType;
	}
	
	@XmlAttribute(name="lang", namespace=Namespaces.XML, required=true)
	public String getLang() {
		return lang;
	}

	public void setLang(String lang) {
		this.lang = lang;
	}
	
	@XmlElement(name="document-target", namespace=Namespaces.DOCUMENT)
	public List<DocumentTarget> getTargets() {
		if(targets == null)
			targets = new ArrayList<DocumentTarget>();
		return targets;
	}
	
	public void setTargets(List<DocumentTarget> targets) {
		this.targets = targets;
	}
	
}
